package com.xbongbong.paas.controller.mobile.workflow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ProcessTypeListDTO;
import com.xbongbong.paas.pojo.vo.ProcessTypeListVO;
import com.xbongbong.paas.service.workflow.approveListPage.WorkflowListSearchService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 移动端审批筛选
 * @author 魏荣杰
 *
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/workflow/search")
public class MobileWorkflowListSearchController {

    @Resource
    private WorkflowListSearchService workflowListSearchService;

    /**
     * 流程状态筛选项
     * @param processTypeListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/typeFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String typeList(@RequestBody @Valid ProcessTypeListDTO processTypeListDTO, BindingResult br) {
        XbbResponse<ProcessTypeListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessTypeListVO processTypeListVO = workflowListSearchService.getProcessTypeList(processTypeListDTO);
                response = new XbbResponse<>(processTypeListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
